package com.simon.app.october.modules.job.controller;

import com.simon.app.october.common.annotation.SysLog;
import com.simon.app.october.common.utils.Constant;
import com.simon.app.october.common.utils.PageUtil;
import com.simon.app.october.common.utils.QueryPage;
import com.simon.app.october.common.utils.Result;
import com.simon.app.october.modules.job.entity.ScheduleJobEntity;
import com.simon.app.october.modules.job.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/sys/schedule")
public class ScheduleJobController {
    @Autowired
    private ScheduleService scheduleService;

    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageUtil page = scheduleService.getPageByParams(new QueryPage(params));
        return Result.ok().put("page", page);
    }

    @GetMapping("/info/{jobId}")
    public Result info(@PathVariable("jobId") Long jobId) {
        ScheduleJobEntity entity = scheduleService.findById(jobId);
        return Result.ok().put("schedule", entity);
    }

    @PostMapping("/save")
    @SysLog("新增定时任务")
    public Result save(@RequestBody ScheduleJobEntity entity)
    {
        scheduleService.save(entity);
        return Result.ok();
    }

    @PostMapping("/update")
    @SysLog("更新定时任务")
    public Result update(@RequestBody ScheduleJobEntity entity) {
        scheduleService.update(entity);
        return Result.ok();
    }

    @PostMapping("/delete")
    @SysLog("删除定时任务")
    public Result delete(@RequestBody Long[] jobIds) {
        scheduleService.delete(jobIds);
        return Result.ok();
    }

    @PostMapping("/run")
    @SysLog("立即执行定时任务")
    public Result run(@RequestBody Long[] jobIds) {
        scheduleService.run(jobIds);
        return Result.ok();
    }

    @PostMapping("/pause")
    @SysLog("暂停定时任务")
    public Result pause(@RequestBody Long[] jobIds) {
        scheduleService.pause(jobIds);
        return Result.ok();
    }

    @PostMapping("/resume")
    @SysLog("恢复定时任务")
    public Result resume(@RequestBody Long[] jobIds) {
        scheduleService.resume(jobIds);
        return Result.ok();
    }
}
